<template>
  <div class="big-file-uploader-container">
    <el-button v-if="progress == 0" @click.native="openDialog"
      >上传文件
    </el-button>
    <el-progress
      v-else
      :width="36"
      :show-text="false"
      :percentage="progress"
      type="circle"
    ></el-progress>
    <el-dialog :visible.sync="dialogVisible" title="上传文件">
      <uppy-dashboard :uppy="uppy"></uppy-dashboard>
    </el-dialog>
  </div>
</template>

<script>
import Uppy from "@uppy/core";
import "@uppy/core/dist/style.css";
import "@uppy/progress-bar/dist/style.css";
import "@uppy/dashboard/dist/style.css";
import { Dashboard } from "@uppy/vue";
export default {
  components: {
    uppyDashboard: Dashboard,
  },
  data() {
    return {
      dialogVisible: false,
      progress: 0,
      uppy: new Uppy({
        debug: true,
        autoProceed: false,
        restrictions: {
          maxNumberOfFiles: 1,
          allowedFileTypes: [
            ".zip",
            ".rar",
            ".7z",
            ".tar",
            ".gz",
            ".bz2",
            ".xz",
          ],
        },
      }),
    };
  },
  methods: {
    openDialog() {
      this.dialogVisible = true;
    },
    closeDialog() {
      this.dialogVisible = false;
    },
  },
  mounted() {
    this.uppy.on("upload", (file, progress) => {
      console.log(progress)
      this.progress = progress.percent;
    });
    this.uppy.on("upload-error", (file, error) => {
      if (error instanceof Uppy.RestrictedFileTypeError) {
        this.$message.error("只能上传文件夹");
      } else {
        this.$message.error("上传失败");
      }
    });
  },
};
</script>
<style lang="less" scoped>
.big-file-uploader-container {
  margin-left: 20px;
}
</style>